package com.xxworkshop.dtcqcalculator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {
    private ListView heroview;
    private ListView rankview;
    private ListView resultview;
    private TextView herocountview;
    private TextView ranknameview;
    private CheckBox flagview;

    private BaseAdapter heroadapter;
    private BaseAdapter resultadapter;

    private DataManager dataManager = DataManager.getInstance();
    private Global global = Global.getInstance();
    private List<Map<String, Object>> resultdata = new ArrayList<Map<String, Object>>();

    private long targetrankid = 0;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        heroview = (ListView) findViewById(R.id.heros);
        rankview = (ListView) findViewById(R.id.ranks);
        resultview = (ListView) findViewById(R.id.result);
        herocountview = (TextView) findViewById(R.id.herocount);
        ranknameview = (TextView) findViewById(R.id.rankname);
        flagview = (CheckBox) findViewById(R.id.flag);

        heroview.setAdapter(heroadapter = new HeroAdapter());
        rankview.setAdapter(new MyAdapter(dataManager.getRanks()));
        resultview.setAdapter(resultadapter = new ResultAdapter());

        heroview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, AddHeroActivity.class);
                intent.putExtra("index", position);
                startActivityForResult(intent, 0);
            }
        });
        rankview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                targetrankid = id;
                ranknameview.setText(((TextView) view.findViewById(R.id.name)).getText());
            }
        });

        Button addview = (Button) findViewById(R.id.add);
        addview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, AddHeroActivity.class);
                intent.putExtra("index", -1);
                startActivityForResult(intent, 0);
            }
        });
        Button calculateview = (Button) findViewById(R.id.calculate);
        calculateview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultdata.clear();
                resultdata.addAll(dataManager.calculate(targetrankid, flagview.isChecked()));
                resultadapter.notifyDataSetChanged();
            }
        });
        Button profileview = (Button) findViewById(R.id.profile);
        profileview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        Button newprofileview = (Button) findViewById(R.id.newprofile);
        newprofileview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                global.profileid = dataManager.getMaxProfileId() + 1;
                global.heros.clear();
                resultdata.clear();
                herocountview.setText("");
                heroadapter.notifyDataSetChanged();
                resultadapter.notifyDataSetChanged();
            }
        });


        if (global.heros.size() > 0) {
            new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);

                    heroadapter.notifyDataSetChanged();
                    herocountview.setText(global.heros.size() + "个英雄");
                }
            }.sendEmptyMessageDelayed(0, 500);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            if (resultCode == 1) {
                heroadapter.notifyDataSetChanged();
                herocountview.setText(global.heros.size() + "个英雄");
                dataManager.setHeroItems(global.profileid, global.heros);
            }
        } else if (requestCode == 1) {
            if (resultCode == 1) {
                heroadapter.notifyDataSetChanged();
                herocountview.setText(global.heros.size() + "个英雄");
                dataManager.setHeroItems(global.profileid, global.heros);
            }
        }
    }

    private class ResultAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return resultdata.size();
        }

        @Override
        public Object getItem(int position) {
            return resultdata.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.listitem, parent, false);
            TextView tv = (TextView) item.findViewById(R.id.name);
            Map<String, Object> data = (Map<String, Object>) getItem(position);
//            tv.setText(data.get("name"));
            tv.setText(data.get("name") + ": " + data.get("count") + "(" + data.get("pieces") + ")(LV" + data.get("requirelevel") + ")");
            return item;
        }
    }

    private class HeroAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return global.heros.size();
        }

        @Override
        public Object getItem(int position) {
            return global.heros.get(position);
        }

        @Override
        public long getItemId(int position) {
            return global.heros.get(position).heroid;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.listitem, parent, false);
            TextView tv = (TextView) item.findViewById(R.id.name);
            HeroItem hi = (HeroItem) getItem(position);
            tv.setText(hi.heroname + " (" + hi.rankname + ")");
            item.setTag(getItemId(position));
            return item;
        }
    }

    private class MyAdapter extends BaseAdapter {
        private List<Map<String, Object>> dataSource;

        public MyAdapter(List<Map<String, Object>> dataSource) {
            this.dataSource = dataSource;
        }

        @Override
        public int getCount() {
            return dataSource.size();
        }

        @Override
        public Object getItem(int position) {
            return dataSource.get(position);
        }

        @Override
        public long getItemId(int position) {
            return Integer.parseInt(dataSource.get(position).get("id").toString());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.listitem, parent, false);
            TextView tv = (TextView) item.findViewById(R.id.name);
            Map<String, Object> data = (Map<String, Object>) getItem(position);
            tv.setText(data.get("name").toString());
            item.setTag(getItemId(position));
            return item;
        }
    }
}
